{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 为什么要微调 Why finetune"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这节课中，我们将学习为什么要进行微调，微调到底是什么。将其与提示词工程进行比较，并通过实验比较一个经过微调的模型和一个非微调模型。\n",
    "\n",
    "那么，为什么要对大模型进行微调呢?在我们讨论原因之前，让我们来认识一下“什么是微调”。微调就是把这些通用的东西像GPT-3这样的模型，并对它们进行专业化变成ChatGPT之类的东西，具体的聊天用例，使它能很好地聊天，或者使用GPT-4，将它变成一个专门的GitHub代码助手，自动完成补全代码。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 目录\n",
    "\n",
    "- 2.1 尝试一个没有经过微调的模型\n",
    "- 2.2 比较一下经过微调的模型\n",
    "- 2.3 与chatPT作比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama import BasicModelRunner"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 尝试一个没有经过微调的模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "non_finetuned = BasicModelRunner(\"meta-llama/Llama-2-7b-hf\", config={\"production.key\": \"<YOUR-KEY-HERE>\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".\n",
      "Tell me how to train my dog to stay.\n",
      "Tell me how to teach my dog to come.\n",
      "Tell me how to get my dog to heel.\n",
      "Tell me how to stop my dog from jumping.\n",
      "Tell me how to make my dog stop barking.\n",
      "Tell me how I can get my dog to stop chewing.\n",
      "Tell me how to house train my dog.\n",
      "Tell me how to potty train my dog.\n",
      "Tell my how to train my dog to walk on a leash.\n",
      "Tell me how to crate train my dog.\n",
      "Tell Me How To Train My Dog To Sit, Stay, Come, Heel, Stop Jumping, Stop Barking, Stop Chewing, House Train, Potty Train, Walk On A Leash, Crate Train, And More!\n",
      "Tell Me How To Train Your Dog To Sit, Stay, Heel, Come, Stop Jumping, Stop Chewing, House Trained, Potty Trained, Walk On A Leash, And More!\n",
      "Tell me how to train your dog to sit.\n",
      "Tell Me How To House Train My Dog.\n"
     ]
    }
   ],
   "source": [
    "print(non_finetuned(\"Tell me how to train my dog to sit\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I think it's a great planet.\n",
      "I think it's a good planet.\n",
      "I think it'll be a great planet.\n",
      "I think we should go there.\n",
      "I think we should go back there.\n",
      "I think we should stay there.\n",
      "I think we should leave there.\n",
      "I think we should colonize there.\n",
      "I think we should terraform there.\n",
      "I think we should mine there.\n",
      "I think we should build there.\n",
      "I think we should live there.\n",
      "I think we should die there.\n",
      "I think we should be there.\n",
      "I think we should have been there.\n",
      "I think we should never be there.\n",
      "I think we'll be there.\n",
      "I think we won't be there.\n",
      "I think we will be there.\n",
      "I think we can be there.\n",
      "I think we could be there.\n",
      "I think we would be there.\n",
      "I think we might be there.\n",
      "I think we may be there.\n",
      "I think we shall be there.\n",
      "I think we must be there.\n",
      "I think we have to be there.\n",
      "I think we need to be there.\n",
      "I think I'll be there.\n",
      "I know I'll\n"
     ]
    }
   ],
   "source": [
    "print(non_finetuned(\"What do you think of Mars?\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm not sure if I've mentioned this before, but I'm a huge Taylor Swift fan. I've been a fan since her first album, and I've been a fan ever since. I've been a fan of her music, her style, her personality, and her music. I've been a fan for a long time.\n",
      "I’ve been a fan of Taylor Swift for a long time. I’ve been a fan of her music for a long time. I’m a fan of her style, her personality, her music, and her music. I’ve been a fan for a long, long time.\n",
      "I’ve always been a fan of Taylor Swift. I’ve always been a fan of her music. I’ve always been a big fan of her style. I’ve always been a huge fan of her personality. I’ve always been a massive fan of her music. I’m a fan of Taylor Swift. I love her music. I love her style. I love her personality. I love her music. I’m a huge fan of Taylor Swift.\n",
      "I’ve always been an avid fan of Taylor Swift. I’m\n"
     ]
    }
   ],
   "source": [
    "print(non_finetuned(\"taylor swift's best friend\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm sorry to hear that but I'm here to help.\n",
      "Customer: I don' t get the blanket\n",
      "Customer: I don't have the blanket\n",
      "Agent: I don't understand.\n",
      "Agent: I'm sorry. I don't understand.\n",
      "customer: I don't have the blanket\n",
      "Agent: I'm so sorry to hear that.\n",
      "Customer: I don t have the blanket\n",
      "Agent: Oh no, I'm so sorry to hear about that.\n",
      "customer: I\n"
     ]
    }
   ],
   "source": [
    "print(non_finetuned(\"\"\"Agent: I'm here to help you with your Amazon deliver order.\n",
    "Customer: I didn't get my item\n",
    "Agent: I'm sorry to hear that. Which item was it?\n",
    "Customer: the blanket\n",
    "Agent:\"\"\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注：从以上的示例中可以看到，没有经过微调的大模型在面对一些很简单的问题的时候，也会出现胡言乱语的情况。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 比较一下经过微调的模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "finetuned_model = BasicModelRunner(\"meta-llama/Llama-2-7b-chat-hf\",config={\"production.key\": \"<YOUR-KEY-HERE>\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on command.\n",
      "How to Train Your Dog to Sit on Command\n",
      "Training your dog to sit on command is a basic obedience command that can be achieved with patience, consistency, and positive reinforcement. Here's a step-by-step guide on how to train your dog to sit on command:\n",
      "1. Choose a Quiet and Distraction-Free Area: Find a quiet area with minimal distractions where your dog can focus on you.\n",
      "2. Have Treats Ready: Choose your dog's favorite treats and have them ready to use as rewards.\n",
      "3. Stand in Front of Your Dog: Stand in front of your dog and hold a treat close to their nose.\n",
      "4. Move the Treat Above Your Dog's Head: Slowly move the treat above your dog's head, towards their tail. As your dog follows the treat with their nose, their bottom will naturally lower into a sitting position.\n",
      "5. Say \"Sit\" and Reward: As soon as your dog's butt touches the ground, say \"Sit\" and give them the treat. It's important to say the command word as they're performing\n"
     ]
    }
   ],
   "source": [
    "print(finetuned_model(\"Tell me how to train my dog to sit\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training your dog to sit is a basic obedience command that can be achieved with patience, consistency, and positive reinforcement. Here's a step-by-step guide on how to train your dog to sit:\n",
      "1. Choose a quiet and distraction-free area: Find a quiet area with no distractions where your dog can focus on you.\n",
      "2. Have treats ready: Choose your dog's favorite treats and have them ready to use as rewards.\n",
      "3. Stand in front of your dog: Stand in front of your dog and hold a treat close to their nose.\n",
      "4. Move the treat up and back: Slowly move the treat up and back, towards your dog's tail, while saying \"sit\" in a calm and clear voice.\n",
      "5. Dog will sit: As you move the treat, your dog will naturally sit down to follow the treat. The moment their bottom touches the ground, say \"good sit\" and give them the treat.\n",
      "6. Repeat the process: Repeat steps 3-5 several times, so your dog starts to associate the command \"sit\" with the action of sitting down.\n",
      "7.\n"
     ]
    }
   ],
   "source": [
    "print(finetuned_model(\"[INST]Tell me how to train my dog to sit[/INST]\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INST]Tell me how to teach my dog to sit[/INST][/INST]\n",
      "[INST]How to teach my dog to sit[INST]\n",
      "[INST]How do I teach my dog to sit[INST][/INST]\n",
      "[inst]How to teach my dog to stay[/inst]\n",
      "[inst]How to train my dog to stay[/inst][/inst]\n",
      "[inst]Tell me how to train a dog to stay[/inst]\n"
     ]
    }
   ],
   "source": [
    "print(non_finetuned(\"[INST]Tell me how to train my dog to sit[/INST]\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mars is a fascinating planet that has captured the imagination of humans for centuries. It is the fourth planet from the Sun in our solar system and is known for its reddish appearance. Mars is a rocky planet with a thin atmosphere, and its surface is characterized by volcanoes, canyons, and impact craters.\n",
      "One of the most intriguing aspects of Mars is its potential for supporting life. While there is currently no evidence of life on Mars, the planet's atmosphere and geology suggest that it may have been habitable in the past. NASA's Curiosity rover has been exploring Mars since 2012, and has discovered evidence of water on the planet, which is a key ingredient for life.\n",
      "Mars is also a popular target for space missions and future human settlements. Several space agencies and private companies are planning missions to Mars in the coming years, with the goal of establishing a human presence on the planet. The challenges of establishing a human settlement on Mars are significant, including the harsh environment, lack of resources, and distance from Earth. However, many experts believe that Mars is the next logical step in the expl\n"
     ]
    }
   ],
   "source": [
    "print(finetuned_model(\"What do you think of Mars?\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Taylor Swift's best friend is a person who has been by her side through thick and thin. Here are some possible candidates:\n",
      "\n",
      "1. Abigail Anderson - Abigail is Taylor's childhood friend and has been a constant presence in her life. The two have been inseparable since they met in kindergarten and have shared countless memories together.\n",
      "2. Selena Gomez - Selena and Taylor have been friends for over a decade and have been through a lot together. They have collaborated on music projects, gone on vacation together, and have been there for each other through personal struggles.\n",
      "3. Liz Rose - Liz is a songwriter and producer who has worked with Taylor on many of her hit songs. The two have a close creative partnership and have been friends for many years.\n",
      "4. Joe Jonas - Joe and Taylor were friends for several years before they started dating in 2008. They have been on-again, off-again since then and have been there for each other during some of the most significant moments in each other's lives.\n",
      "5. Ed Sheeran - Ed and Taylor have been friends\n"
     ]
    }
   ],
   "source": [
    "print(finetuned_model(\"taylor swift's best friend\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I see. Can you please provide me with your order number so I can look into this for you?\n",
      "Customer: I don't have the order number.\n",
      "Agent: Okay, no worries. Can you please tell me the name of the item you didn't receive?\n",
      "Customer: Blanket\n",
      "Agent: And what was the shipping address for this item?\n",
      "Customer: 123 Main St\n",
      "Agent: Okay, thank you. I'm going to check on the status of your order. Can you please hold for just a moment?\n",
      "Customer: (hangs up)\n",
      "This is a common scenario in customer service, where the agent is trying to gather information from the customer to help resolve their issue. The agent is using active listening skills to gather information from the customer, such as the order number, the name of the item, and the shipping address. The agent is also using empathy to acknowledge the customer's concern and let them know that they are there to help.\n"
     ]
    }
   ],
   "source": [
    "print(finetuned_model(\"\"\"Agent: I'm here to help you with your Amazon deliver order.\n",
    "Customer: I didn't get my item\n",
    "Agent: I'm sorry to hear that. Which item was it?\n",
    "Customer: the blanket\n",
    "Agent:\"\"\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注：经过微调的大模型，回答的就比较好，最起码是回答了问题的内容。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 与chatPT作比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "chatgpt = BasicModelRunner(\"chat-gpt\", config={\"production.key\": \"<YOUR-KEY-HERE>\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training a dog to sit is a basic command that can be taught using positive reinforcement techniques. Here's a step-by-step guide on how to train your dog to sit:\n",
      "\n",
      "1. Choose a quiet and distraction-free environment: Find a calm area in your home or a quiet outdoor space where your dog can focus on the training without any distractions.\n",
      "\n",
      "2. Gather treats: Use small, soft, and tasty treats that your dog loves. These treats will serve as rewards during the training process.\n",
      "\n",
      "3. Get your dog's attention: Call your dog's name or use a clicker to get their attention. Make sure they are looking at you before proceeding.\n",
      "\n",
      "4. Lure your dog into a sitting position: Hold a treat close to your dog's nose and slowly move it upwards and slightly backward over their head. As their nose follows the treat, their bottom will naturally lower into a sitting position. Once they are sitting, say \"sit\" in a clear and firm voice.\n",
      "\n",
      "5. Reward and praise: As soon as your dog sits, give them the treat and offer verbal praise such as \"good sit\" or \"well done.\" This positive reinforcement will help them associate the action with the command.\n",
      "\n",
      "6. Repeat the process: Practice the sit command multiple times in short training sessions\n"
     ]
    }
   ],
   "source": [
    "print(chatgpt(\"Tell me how to train my dog to sit\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注：当然chatgpt可以做到更好。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gpt",
   "language": "python",
   "name": "gpt"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
